package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qingcheng.dao.AlbumMapper;
import com.qingcheng.entity.PageResult;
import com.qingcheng.pojo.goods.Album;
import com.qingcheng.service.goods.AlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AlbumServiceImpl implements AlbumService {

    @Autowired
    private AlbumMapper albumMapper;

    /* */

    /**
     * 批量上传图片
     *
     * @param imageUrls
     * @param id
     */
    @Override
    public void addMany(ArrayList<Object> imageUrls, int id) {

        System.out.println(imageUrls);
        //1. 根据id 查询 相册对象
        Album album = albumMapper.selectByPrimaryKey(id);

        // 1.     [{"url":"http://localhost:9101/img/1.jpg"},{"url":"http://localhost:9101/img/7.jpg"}]
        //2.      [{"url":"11111","status":"success"}, {"url":"22222"},"status":"success"]

        // 3.      [{"url":"", "status":"success"},{"url":"", "status":"success"}]
        // 遍历集合
        ArrayList<Object> urlList = new ArrayList();

        for (int i = 0; i < imageUrls.size(); i++) {
            Map<String, String> urlMap = (Map<String, String>) imageUrls.get(i);
            String response = urlMap.get("response");
            Map map = new HashMap();
            map.put("url", response);    //{"url":"111"}

            map.put("status", "success");             // value {"url":"111", "status":"success"}
            urlList.add(map);        //将新生成的集合 添加到集合中   [{"url":"111", "status":"success"}]
        }

        System.out.println(urlList);
        String string = JSON.toJSONString(urlList);
        album.setImageItems(string);

        albumMapper.updateByPrimaryKeySelective(album);

        System.out.println(" ======nihao 哈就222222");
    }

    /**
     * 返回全部记录
     *
     * @return
     */
    public List<Album> findAll() {
        return albumMapper.selectAll();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 每页记录数
     * @return 分页结果
     */
    public PageResult<Album> findPage(int page, int size) {
        PageHelper.startPage(page, size);
        Page<Album> albums = (Page<Album>) albumMapper.selectAll();
        return new PageResult<Album>(albums.getTotal(), albums.getResult());
    }

    /**
     * 条件查询
     *
     * @param searchMap 查询条件
     * @return
     */
    public List<Album> findList(Map<String, Object> searchMap) {
        Example example = createExample(searchMap);
        return albumMapper.selectByExample(example);
    }

    /**
     * 分页+条件查询
     *
     * @param searchMap
     * @param page
     * @param size
     * @return
     */
    public PageResult<Album> findPage(Map<String, Object> searchMap, int page, int size) {
        PageHelper.startPage(page, size);
        Example example = createExample(searchMap);
        Page<Album> albums = (Page<Album>) albumMapper.selectByExample(example);
        return new PageResult<Album>(albums.getTotal(), albums.getResult());
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    public Album findById(Long id) {
        Album album = albumMapper.selectByPrimaryKey(id);
        return album;
    }

    /**
     * 新增
     *
     * @param album
     */
    public void add(Album album) {
        albumMapper.insert(album);
    }

    /**
     * 修改
     *
     * @param album
     */
    public void update(Album album) {
        albumMapper.updateByPrimaryKeySelective(album);
    }

    /**
     * 删除相册列表中的 url
     *
     * @param id
     */
    public void delete(ArrayList<String> imageList, Long id) {
        //1.  根据id查询album 对象
        Album album = albumMapper.selectByPrimaryKey(id);
        String imageItems = album.getImageItems(); // [{"url":" ","status":"success"},{}]

        //2. 将json字符串转成list集合
        List<Map<String, String>> parse = (List<Map<String, String>>) JSON.parse(imageItems);

        if (parse != null) {
            //遍历 集合
            boolean flag=false;
            for (int i = parse.size()-1; i >=0; i--) {
                //获取每一个 url 集合
                Map<String, String> map = parse.get(i);
                String url = map.get("url");

                //遍历传入的参数
                for (String imageUrl : imageList) {
                    //如果传入的url在 数据库中存在,,则删除 这个url所在的map
                    if (url.equals(imageUrl)) {
                        parse.remove(map);  //移除该map

                    }
                }


            }
        }
        //3. 将 修改后的 imageItems  重新封装到对象  保存到数据库
        imageItems = JSON.toJSONString(parse);
        album.setImageItems(imageItems);

        albumMapper.updateByPrimaryKeySelective(album);
    }

    /**
     * 构建查询条件
     *
     * @param searchMap
     * @return
     */
    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Album.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 相册名称
            if (searchMap.get("title") != null && !"".equals(searchMap.get("title"))) {
                criteria.andLike("title", "%" + searchMap.get("title") + "%");
            }
            // 相册封面
            if (searchMap.get("image") != null && !"".equals(searchMap.get("image"))) {
                criteria.andLike("image", "%" + searchMap.get("image") + "%");
            }
            // 图片列表
            if (searchMap.get("imageItems") != null && !"".equals(searchMap.get("imageItems"))) {
                criteria.andLike("imageItems", "%" + searchMap.get("imageItems") + "%");
            }


        }
        return example;
    }

}
